extends Node2D

var max_speed = 3000
var acc_speed = 10000
var direction: Vector2 = Vector2.ZERO
var acc: Vector2 = Vector2.ZERO
var vel: Vector2 = Vector2.ZERO

var to_zoom: Vector2 = Vector2.ZERO

onready var hud = $HUD

onready var feichuan = $"飞船"
onready var particles = $"飞船/Particles2D"
onready var camera = $"飞船/Camera2D"
onready var nfx = $NegativeFX
onready var stars = $Stars
onready var nfxs = $NFXs

var star_prefab = preload("res://TSCN/物体/星.tscn")
var hui_prefab = preload("res://TSCN/物体/回.tscn")

func clear_stars():
	for node in stars.get_children():
		var star: RigidBody2D = node
		var dis = star.global_position.y -feichuan.global_position.y
		if dis > 3000:
			star.queue_free()
			
func clear_nfxs():
	for node in nfxs.get_children():
		var nfx: ColorRect = node
		var dis = nfx.rect_position.y -feichuan.global_position.y
		if dis > 3000:
			nfx.queue_free()

func _process(delta: float) -> void:
	clear_stars()
	clear_nfxs()
	
	hud.progress = -feichuan.global_position.y / 100000
	
	var velocity = vel + Vector2(0, -1000)
	
	if hud.progress > 1:
		
		if randf() < 0.5 * delta:
			# 生成回（虫洞）
			var hui: RigidBody2D = hui_prefab.instance()
			stars.add_child(hui)
			hui.set_global_position(feichuan.global_position + Vector2(0, -4000))
			
			for node in hui.get_children():
				if "scale" in node:
					node.scale *= 3.0
		
		pass
	else: # 生成天体
		if randf() < 2.0 * delta:
			var nfx_rect: ColorRect = nfx.duplicate()
			nfxs.add_child(nfx_rect)
			nfx.set_global_position(feichuan.global_position + Vector2(-30000, -30000))
			nfx.rect_scale.y = rand_range(0.05, 0.3) if randf() < 0.8 else rand_range(1, 3)
			
		if randf() < 2.0 * delta:
			var star: RigidBody2D = star_prefab.instance()
			stars.add_child(star)
			star.set_global_position(feichuan.global_position + Vector2(0, -4000))
			
			var rand = rand_range(0.1, 1.0)
			for node in star.get_children():
				node.scale *= rand

	
	
	var particle_mat: ParticlesMaterial = particles.process_material
	var my_dir: Vector2 = -velocity.normalized()
	
	feichuan.rotation = my_dir.angle() - PI/2
	particle_mat.direction.x = my_dir.x
	particle_mat.direction.y = my_dir.y
	
	
	if abs(feichuan.position.x) > 1500:
		feichuan.position.x = 1499 * sign(feichuan.position.x )
	
	feichuan.position += velocity * delta
	
	vel += acc * delta
	vel -= vel * delta
	acc -= acc * delta
	
	if vel.length() > max_speed:
		vel = vel.normalized() * max_speed

	direction.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
	direction.y = - Input.get_action_strength("move_up")
	acc = direction * acc_speed
	
	to_zoom = Vector2(2, 2) *(1 + vel.length() / 3000)
	camera.zoom = camera.zoom.move_toward(to_zoom, delta)
	
	camera.position.y = -600 * (camera.zoom.x - 1)

func _ready() -> void:
	Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)

var end = false

func _on_StaticBody_body_entered(body: Node) -> void:
	if "回" in body.name:
		if not end:
			end = true
			var anim: AnimationPlayer = body.get_node("AnimationPlayer")

			anim.play("play", -1, 0.2)
			yield(anim, "animation_finished")
			# 过关
			Globals.load_scene(Globals.Level2)
		return
	
	Globals.reload_scene()
	
	pass
